/*
 * @Author       : xiangmin
 * @File         : 上传图片公用
 * @Date         : 2023-10-31 18:50:42
 * @LastEditors  : xiangmin
 * @LastEditTime : 2023-10-31 18:53:55
 */
import { Upload, type UploadProps } from 'ant-design-vue'

export default function (accept: string, maxSize: number | string) {
	// 上传前格式检查
	const beforeUploadFile: UploadProps['beforeUpload'] = file => {
		const { size, name, type } = file
		const isAccept = type && accept.includes(type)
		if (!isAccept) {
			message.error(`${name} 格式不对，只能传${accept}`)
			return Upload.LIST_IGNORE
		}
		const isOverSize = size < +maxSize * 1024 * 1024
		if (!isOverSize) {
			message.error(`${name} 超过限制大小：${maxSize}M`)
			return Upload.LIST_IGNORE
		}
	}

	return { beforeUploadFile }
}
